package edu.friday.service.impl;

import edu.friday.model.SysRole;
import edu.friday.repository.SysRoleRepositery;
import edu.friday.service.SysRoleService;
import edu.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleRepositery sysRoleRepositery;

    @Override
    public List<SysRole> selectRolelist() {
        return sysRoleRepositery.findAll();
    }

    @Override
    public List<SysRole> selectRoleAll() {
        return sysRoleRepositery.findAll();
    }

    @Override
    public List<Long> selectRoleListByUserId(Long userId) {
        return sysRoleRepositery.selectRoleIdsByUserId(userId);
    }

    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        List<Long> roleIds = sysRoleRepositery.selectRoleIdsByUserId(userId);
        Set<String> permissionSet = new HashSet<>();
        for (Long roleId : roleIds) {
            SysRole role = sysRoleRepositery.findById(roleId).get();
            String perms=role.getRoleKey();
            if (StringUtils.isNotEmpty(perms)) {
                permissionSet.addAll(Arrays.asList(perms.split(",")));
            }
        }
        return permissionSet;
    }


}
